import 'package:citiesocial/_include.dart';
import 'package:citiesocial/widgets/goods/horizonal_goods.dart';
// import 'package:html2md/html2md.dart' as html2md;
// import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:flutter_html/flutter_html.dart';

class GoodsDetail extends BaseStatefulWidget {
  final GoodsEntity goods;

  GoodsDetail(this.goods) : super();

  @override
  State<StatefulWidget> createState() => _GoodsDetailState();
}

class _GoodsDetailState extends BaseState<GoodsDetail> {
  GoodsDetailEntity detail;
  _GoodsDetailState() : super();

  void _loadDetail() async {
    LoadingDialog.show(context, barrierColor: const Color(0x01000000));
    if (widget.goods == null) return;
    final deviceId = await getDeviceId();
    final goodsDetail =
        await HttpManager.goodsDetail('${widget.goods.goods_id}', deviceId);
    await LoadingDialog.dismiss(context);
    if (goodsDetail != null &&
        goodsDetail.goods_id != null &&
        goodsDetail.goods_id.length > 0) {
      detail = goodsDetail;
      widget.goods.brand_name = goodsDetail.goods_brand;
      widget.goods.goods_name = goodsDetail.goods_name;
      widget.goods.is_focus = goodsDetail.is_focus;
      setState(() {});
    } else {
      Toast.show(context, '获取详情失败');
    }
  }

  ///静默添加商品浏览历史
  void _silentAddToHistory() async {
    final deviceId = await getDeviceId();
    HttpManager.addHistoryGoods(deviceId, widget.goods?.goods_id);
  }

  @override
  void initState() {
    Future.delayed(Configs.loadDelay, () {
      _loadDetail();
      _silentAddToHistory();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final view = Scaffold(
      appBar: AppBarUtils.detail(context, goods: widget.goods, callback: () {
        setState(() {});
      }),
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Expanded(
              child: ListView(
                children: _generateChildren(),
              ),
            ),
            GestureDetector(
              child: Container(
                width: double.infinity,
                padding: EdgeInsets.all(15.0),
                color: Configs.main,
                child: Center(
                  child: Text(
                    '加入购物车',
                    style: TextStyle(fontSize: 18.0, color: Colors.white),
                  ),
                ),
              ),
              onTap: () async {
                Configs.mayLogin(context).then((yes) async {
                  if (yes) {
                    HttpManager.addCart(Configs.userId, Configs.userToken,
                            widget.goods.goods_id,
                            context: context)
                        .then((yes) {
                      if (yes) {
                        //Toast.show(context, '添加购物车成功');
                        setState(() {
                          Configs.saveCartCount(Configs.cartCount + 1);
                        });
                      }
                    });
                  }
                });
              },
            ),
          ],
        ),
      ),
    );
    return view;
  }

  List<Widget> _generateChildren() {
    final views = List<Widget>();
    if (detail != null) {
      views.add(BannerImage(detail.goods_img));
      // views.add(MarkdownBody(data: html2md.convert('${detail.goods_desc}')));
      views.add(Html(
        data: '${detail.goods_desc}',
      ));
      if (detail.recommended_goods != null &&
          detail.recommended_goods.length > 0) {
        views.add(Container(
          child: Text(
            '推荐商品',
            style: TextStyle(fontSize: 20.0, color: Colors.black),
          ),
          padding: EdgeInsets.all(10.0),
        ));

        views.add(Container(
          child: Configs.dividerLine,
          margin: EdgeInsets.symmetric(horizontal: 100.0),
        ));

        views.add(HorizonalGoods(detail.recommended_goods));
      }
    } else {
      views.add(LoadingDialog());
    }
    return views;
  }
}
